package com.itheima.service.impl;

import com.itheima.mapper.ChatMapper;
import com.itheima.pojo.Chat;
import com.itheima.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ChatServiceImpl implements ChatService {
    @Autowired
    private ChatMapper chatMapper;

    @Override
    public void send(Chat chat) {
        chatMapper.insert(chat);
    }

    @Override
    public List<Chat> getChatList(Chat chat) {
        Integer ownerId = chat.getSendId();
        Integer otherId = chat.getImbibeId();
        List<Chat> chatList = chatMapper.getChatList(chat);
        chat.setSendId(otherId);
        chat.setImbibeId(ownerId);
        List<Chat> chatList2 = chatMapper.getChatList(chat);
        chatList.addAll(chatList2);
        for (int i = 0; i <chatList.size()-1; i++) {
            for (int j =0; j <chatList.size()-i-1 ; j++) {
                if (chatList.get(j+1).getSendTime().isBefore(chatList.get(j).getSendTime())){
                    Chat temp=chatList.get(j+1);
                    chatList.set(j+1,chatList.get(j));
                    chatList.set(j,temp);
                }
            }
        }
        return chatList;
    }
}
